package com.baidu.aip;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;

import com.baidu.aip.face.camera.ICameraControl;

/**
 * @author kzcai
 * @packageName com.baidu.aip
 * @date 1/14/21
 */
public class MainFaceDecActivity extends Activity {

    private Activity activity;
    private int cameraFacing = ICameraControl.CAMERA_FACING_BACK;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = this;
        cameraFacing = getIntent().getIntExtra("cameraFacing",cameraFacing);
        registerBoradcastReceiver();
        setWindow();
        Intent intent = new Intent(this,FaceDetectActivity.class);
        intent.putExtra("cameraFacing",cameraFacing);
        startActivity(intent);
    }
    private void setWindow(){
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = 0;
        params.height = 0;
        getWindow().setAttributes(params);
    }

    public void registerBoradcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(getPackageName()+".faceReceive");
        //注册广播
        registerReceiver(mBroadcastReceiver, myIntentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBroadcastReceiver);
    }

    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(getPackageName()+".faceReceive")){
                byte[] image = intent.getByteArrayExtra("data");
                Intent intent1 = new Intent(getPackageName()+".faceReceive");
                intent1.putExtra("data", image);
                activity.setResult(RESULT_OK, intent1);
                finish();
            }
        }

    };
}
